package com.zb.listener;

import com.alibaba.fastjson.JSON;
import com.zb.config.MQConfig;
import com.zb.entity.TbSeckillGoodsModel;
import com.zb.entity.TbSeckillOrderModel;
import com.zb.service.TbSeckillOrderService;
import com.zb.vo.SeckillStatus;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * <p>添加描述信息</p>
 *
 * @author 李柯
 * @since 2022年11月18日 14:20
 */
@Component
public class MQListener {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TbSeckillOrderService orderService;


    @RabbitListener(queues = MQConfig.SECKILL_QUEUE)
    public void revicePaySuccess(String json)throws Exception{
        System.out.println("========");
//        {"transaction_id":"4200001668202211186010881967","nonce_str":"eb13411500064505a48b65fc282eb395","bank_type":"OTHERS","openid":"oHkLxt26N-Oq4SpVKy8U2fOPimcg","sign":"BA0B9805AA4934FBE3C012D048A786B5","fee_type":"CNY","mch_id":"11473623","cash_fee":"1","out_trade_no":"15906002796219269139090","appid":"wxab8acb865bb1637e","total_fee":"1","trade_type":"NATIVE","result_code":"SUCCESS","attach":"{\"flag\":\"2\",\"username\":\"admin1\"}","time_end":"20221118142925","is_subscribe":"N","return_code":"SUCCESS"}
        Map<String,String> data = JSON.parseObject(json, Map.class);
        String attachJson = data.get("attach");
        Map<String, String> attachMap = JSON.parseObject(attachJson, Map.class);
        TbSeckillOrderModel orderModel = (TbSeckillOrderModel) redisTemplate.boundHashOps("seckillOrder").get(attachMap.get("username"));
        orderService.save(orderModel);
        SeckillStatus seckillStatus = (SeckillStatus) redisTemplate.boundHashOps("seckillStatus").get(attachMap.get("username"));
        seckillStatus.setStatus(5);
        redisTemplate.boundHashOps("seckillStatus").put(attachMap.get("username"),seckillStatus);
        redisTemplate.boundHashOps("userCount").delete(attachMap.get("username"));
        System.out.println(json);

    }

    @RabbitListener(queues = MQConfig.TWOSECKILL_QUEUE)
    public void reviceNotPay(String username){
        System.out.println("回滚");
        redisTemplate.boundHashOps("seckillOrder").delete(username);
        SeckillStatus seckillStatus = (SeckillStatus) redisTemplate.boundHashOps("seckillStatus").get(username);
//        if (seckillStatus.getStatus()==5){
//            return;
//        }
        seckillStatus.setStatus(3);
        redisTemplate.boundHashOps("seckillStatus").put(username,seckillStatus);
        redisTemplate.boundHashOps("userCount").delete(username);
        TbSeckillGoodsModel seckillGoodsModel = (TbSeckillGoodsModel) redisTemplate.boundHashOps("seckillGoods:" + seckillStatus.getTime()).get(seckillStatus.getGoodsId());
        seckillGoodsModel.setStockCount(seckillGoodsModel.getStockCount() + 1);
        redisTemplate.boundHashOps("seckillGoods:" + seckillStatus.getTime()).put(seckillStatus.getGoodsId(), seckillGoodsModel);
        redisTemplate.boundListOps("seckillGoodsCountList:" + seckillStatus.getGoodsId()).leftPush(seckillStatus.getGoodsId());

    }

}
